package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.PortalApplyInfo;
import cn.com.yx.ms.service.IPortalService;
import cn.com.yx.ms.service.ISysService;
import cn.com.yx.ms.service.ITestService;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

// import org.java_websocket.drafts.Draft_17;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "portal")
@Api(value = "门户接口", description = "用于门户网站的业务交互")
public class PortalController {

    private static final Logger logger = LoggerFactory.getLogger(PortalController.class);

    @Resource
    private ITestService iTestService;
    @Resource
    private IPortalService iPortalService;
    @Resource
    private ISysService iSysService;
    @PostMapping("applyInfo")
    @ApiOperation(value = "创建使用申请", notes = "创建使用申请")
    @ApiImplicitParam(paramType = "body", name = "testmode", dataType = "Map", required = true, value = "创建使用申请")
    public ApiResult create(@RequestBody @Validated PortalApplyInfo portalApplyInfo,HttpServletRequest request) {
        ApiCode apiCode=iPortalService.saveApplyInfo(portalApplyInfo,portalApplyInfo.getUuid());
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_400501),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @GetMapping("applyInfo/page")
    @ApiOperation(value = "查询使用申请", notes = "查询使用申请")
    @ApiImplicitParam(paramType = "body", name = "portalApplyInfo", dataType = "Map", required = true, value = "查询使用申请")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request){
        query.getConditions().put("guid",request.getParameter("guid"));
        query.getConditions().put("mobile",request.getParameter("mobile"));
        query.getConditions().put("username",request.getParameter("username"));
        query.getConditions().put("createtime",request.getParameter("createtime"));
        query.getConditions().put("company",request.getParameter("company"));
        query.getConditions().put("state",request.getParameter("state"));
        query.getConditions().put("pi",request.getParameter("pi"));
        query.getConditions().put("ps",request.getParameter("ps"));
        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<PortalApplyInfo> page = (Page<PortalApplyInfo>) iPortalService.find(query);
        ApiPager<PortalApplyInfo> apiPager = new ApiPager<PortalApplyInfo>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());
        return new ApiResult<>(AppConstant.MSG_SUCCESS,true,apiPager);
    }
    @PutMapping("applyInfo/{guid}")
    @ApiOperation(value = "修改使用申请状态", notes = "修改使用申请状态")
    @ApiImplicitParam(paramType = "body", name = "portalApplyInfo", dataType = "Map", required = true, value = "修改使用申请")
    public ApiResult updateProtal(@PathVariable("guid")String guid){
        ApiCode apiCode=iPortalService.updateByGuid(guid);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_400500),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }
}